package juc.cas;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @ClassName : AtomicReferenceDemo
 * @Author : hyy
 * @Date: 2020-03-11
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
class User{
    private int age;
    private String name;
}

public class AtomicReferenceDemo {
    public static void main(String[] args) {
        AtomicReference<User> userAtomicReference = new AtomicReference<User>();
        User zs = new User(23,"zs");
        User ls = new User(24,"ls");
        // compareAndSet(var1,var2) var1:期待值，var2：修改值；如果满足期待值，将其修改为修改值，并返回true,否则饭后false
        // true
        System.out.println(userAtomicReference.compareAndSet(null,zs));
        // false
        System.out.println(userAtomicReference.compareAndSet(null,ls));
        // true
        System.out.println(userAtomicReference.compareAndSet(zs,ls));
    }
}
